﻿using Common.Communication;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Common.Tool.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Common.Device.Models
{
    public class Fct : Device
    {
        /// <summary>
        /// 测试模式
        /// </summary>
        public FctEnum.TestMode mode { get; set; }
        public string 闭合通道 { get; set; }
        public string 断开通道 { get; set; }
        public int 电平通道 { get; set; }
        public string 期望状态 { get; set; }
        /// <summary>
        /// 获取输入的继电器状态
        /// </summary>
        /// <param name="jdq">支持的继电器数量</param>
        /// <returns></returns>
        public List<bool?> GetJdqChannel(int jdq)
        {
            List<bool?> channels = new List<bool?>();
            for (int i = 1; i < jdq + 1; i++)
            {
                channels.Add(null);
                if (GetTextChannel(断开通道).Contains(i))
                {
                    channels[i - 1] = false;
                }
                if (GetTextChannel(闭合通道).Contains(i))
                {
                    channels[i - 1] = true;
                }
            }
            return channels;
        }
        /// <summary>
        /// 将输入的字符串转换为通道号
        /// </summary>
        /// <param name="txt">继电器 1-3,5,7-9</param>
        /// <returns></returns>
        public List<int> GetTextChannel(string txt)
        {
            try
            {
                List<int> channels = new List<int>();
                if (txt == "")
                {
                    return channels;
                }
                //拆分string
                string[] str = txt.Split(',');
                //遍历
                for (int i = 0; i < str.Length; i++)
                {
                    //连续通道号
                    if (str[i].Contains("-"))
                    {
                        string[] num = str[i].Split('-');
                        int first = Convert.ToInt32(num[0]);
                        int last = Convert.ToInt32(num[1]);
                        for (int j = first; j < last + 1; j++)
                        {
                            channels.Add(j);
                        }
                    }
                    //间隔通道号
                    else
                    {
                        int channel = Convert.ToInt32(str[i]);
                        channels.Add(channel);
                    }
                }
                return channels;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 获取测试指令
        /// </summary>
        /// <param name="deviceName">仪器名</param>
        /// <returns></returns>
        public override Order GetOrder(string deviceName)
        {
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (mode)
            {
                case FctEnum.TestMode.继电器控制:
                    order.deviceModel = JsonConvert.SerializeObject(new { 闭合通道, 断开通道 });
                    break;
                case FctEnum.TestMode.电平采集:
                    order.range = 期望状态;
                    order.deviceModel = JsonConvert.SerializeObject(new { 电平通道, 期望状态 });
                    break;
                default:
                    break;
            }
            return order;
        }
        /// <summary>
        /// 测试并返回测试结果
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="testOrder"></param>
        /// <returns></returns>
        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {

            //var device = dev as IFct;
            var result = new DeviceResult();
            //测试结果实体
            Result<bool> r1 = new Result<bool>();
            Result<bool> r2 = new Result<bool>();
            int jdq = 0;
            //通过仪器通信参数获取继电器数量
            if (dev is IFct)
            {
                if (!string.IsNullOrEmpty(DeviceService.useDevices[(dev as IFct).Name].parameter2))
                {
                    string[] channel = DeviceService.useDevices[(dev as IFct).Name].parameter2.Split(';');
                    if (channel.Length >= 1)
                    {
                        jdq = int.Parse(channel[0]);
                    }
                }
            }
            if (dev is IFct3)
            {
                if (!string.IsNullOrEmpty(DeviceService.useDevices[(dev as IFct3).Name].parameter2))
                {
                    string[] channel = DeviceService.useDevices[(dev as IFct3).Name].parameter2.Split(';');
                    if (channel.Length >= 1)
                    {
                        jdq = int.Parse(channel[0]);
                    }
                }
            }
            switch (mode)
            {
                case FctEnum.TestMode.继电器控制:
                    if (dev is IFct)
                    {
                        r1 = (dev as IFct).SetJdq(GetJdqChannel(jdq));
                    }
                    if (dev is IFct3)
                    {
                        List<int> closeChannels = GetTextChannel(闭合通道);
                        List<int> openChannels = GetTextChannel(断开通道);
                        r1 = (dev as IFct3).SetJdq(closeChannels, openChannels);
                    }
                    result.success = r1.IsSucceed;
                    result.result = r1.IsSucceed ? "OK" : r1.Err;
                    break;
                case FctEnum.TestMode.电平采集:
                    if (dev is IFct)
                    {
                        r2 = (dev as IFct).GetDp(电平通道);
                    }
                    result.success = r2.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r2.Err;
                        break;
                    }
                    result.result = r2.Value ? "高电平" : "低电平";
                    result.success = 期望状态 == result.result;
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
